package com.polaris.live.popup.party

import android.content.Context
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.party.InteractionEmojiAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.BizEventConst
import com.polaris.live.common.event.EventMessage
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.DebounceHelper
import com.polaris.live.databinding.PopupInteractionEmojiBinding
import com.polaris.live.dialog.VipEmojiDialog
import com.polaris.live.dialog.common.ConfirmCanalDetailDialog
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.LiveEmojiResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.RichCharmImgUtil
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import org.greenrobot.eventbus.EventBus

/**
 * 互动emoji列表
 *
 * @author Created by 天晴 on 2024/2/27 19:18
 * @since 1.0.0
 **/
class InteractionEmojiPopup(
    context: Context,
    private val mEmojiList: List<LiveEmojiResp>,
    private val mViewModel: BaseViewModel,
    private val mOnItemClickListener: ((LiveEmojiResp) -> Unit)? = null,
    private val removeList: (() -> Unit)? = null,  //更新list
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupInteractionEmojiBinding
    override fun getImplLayoutId() = R.layout.popup_interaction_emoji

    private val mAdapter by lazy { InteractionEmojiAdapter(lifecycleScope) }

    private val mLayoutManager by lazy {
        GridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false)
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupInteractionEmojiBinding.bind(popupImplView)
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = mLayoutManager
        }

        mAdapter.setList(mEmojiList)

        mAdapter.setOnItemClickListener { _, _, position ->
            DebounceHelper.singleClick {
                val item = mAdapter.getItemOrNull(position) ?: return@singleClick
                if (item.available.toBoolean()) {
                    mOnItemClickListener?.invoke(item)
                } else {
                    if (item.type == EMOJI_RICH) {
                        val commonRichImg = RichCharmImgUtil.getCommonRichImg(UserManager.getUser().account?.richs ?: 0)
                        var level = if (commonRichImg.first < 0) {
                            0
                        } else {
                            commonRichImg.first
                        }

                        val message = StringUtils.getString(string.rich_user_emoji, item.objectId, level)
                        ConfirmCanalDetailDialog.show(
                            context, StringUtils.getString(string.warning), message,
                            mCanal = StringUtils.getString(string.ok),
                            mConfirm = StringUtils.getString(string.recharge),
                            onConfirmCallback = {
                                GlobalPopupManager.showRechargePopup(
                                    context,
                                    mViewModel,
                                    isNeedBottom = true,
                                    onRechargeOk = {
                                        EventBus.getDefault().post(EventMessage(BizEventConst.INFO_UPDATA))
                                    }
                                )
                            }
                        )
                        removeList?.invoke()
                    } else if (item.type == EMOJI_VIP) {
                        item.objectId?.let {
                            VipEmojiDialog.show(context, it) {
                                Router.instance.setPath(RoutePath.VIP_CENTER_ACTIVITY)
                                    .navigation(context)
                            }
                        }
                    }
                }
                dismiss()
            }
        }
    }

    companion object {

        const val EMOJI_COMMON = 0      //通用emoji
        const val EMOJI_RICH = 1        //壕等级emoji
        const val EMOJI_VIP = 2         //vipEmoji
    }

}